﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Web;

using BlueCMS.Core.IoC;
using BlueCMS.Core.Providers;
using BlueCMS.Core.Data.Query.Generator;

using System.Linq.Expressions;

namespace BlueCMS.Core.Data
{
    //public class Table<T> : IQueryable<T>, IQueryProvider
    //{
    //    IEnumerator<T> IEnumerable<T>.GetEnumerator()
    //    {
    //        throw new NotImplementedException();
    //    }

    //    IEnumerator IEnumerable.GetEnumerator()
    //    {
    //        return (this as IQueryable).Provider.Execute<IEnumerator<T>>(_expression);
    //    }

    //    Type IQueryable.ElementType
    //    {
    //        get { return typeof(T); }
    //    }

    //    System.Linq.Expressions.Expression IQueryable.Expression
    //    {
    //        get { return Expression.Constant(this); }
    //    }

    //    IQueryProvider IQueryable.Provider
    //    {
    //        get { return this; }
    //    }

    //    #region IQueryProvider Members

    //    IQueryable<TElement> IQueryProvider.CreateQuery<TElement>(Expression expression)
    //    {
    //        throw new NotImplementedException();
    //    }

    //    IQueryable IQueryProvider.CreateQuery(System.Linq.Expressions.Expression expression)
    //    {
    //        throw new NotImplementedException();
    //    }

    //    TResult IQueryProvider.Execute<TResult>(System.Linq.Expressions.Expression expression)
    //    {
    //        MethodCallExpression methodCall = _expression as MethodCallExpression;
    //    }

    //    object IQueryProvider.Execute(System.Linq.Expressions.Expression expression)
    //    {
    //        throw new NotImplementedException();
    //    }

    //    #endregion
    //}

    public class DataAccess
    {
        const string DATA_ACCESS_KEY = "BlueCMS.Core.Data.DATA_ACCESS_KEY";
        static object locker = new object();
 
        IDataProvider Provider { get; set; }

        protected DataAccess()
        {
            Provider = Container.Resolve<IDataProvider>();
        }

        public QueryGeneratorBase GetQueryGenerator()
        {
            return Provider.GetQueryGenerator();
        }

        public DbParameter CreateParameter(string name, object value)
        {
            return Provider.CreateParameter(name, value);
        }

        public int ExecuteNonQuery(string commandText, DbParameter[] parameters)
        {
            return Provider.ExecuteNonQuery(commandText, parameters);
        }

        public int ExecuteNonQuery(string commandText)
        {
            return Provider.ExecuteNonQuery(commandText, null);
        }

        public DbDataReader ExecuteDataReader(string commandText, DbParameter[] parameters)
        {
            return Provider.ExecuteDataReader(commandText, parameters);
        }

        public object ExecuteScalar(string commandText, DbParameter[] parameters)
        {
            return Provider.ExecuteScalar(commandText, parameters);
        }

        public static DataAccess Instance
        {
            get 
            {
                DataAccess instance = HttpContext.Current.Items[DATA_ACCESS_KEY] as DataAccess;
                if (instance == null)
                {
                    lock (locker)
                    {
                        instance = HttpContext.Current.Items[DATA_ACCESS_KEY] as DataAccess;
                        if (instance != null)
                            return instance;

                        instance = new DataAccess();
                        HttpContext.Current.Items[DATA_ACCESS_KEY] = instance;
                    }
                }
                return instance; 
            }
        }
    }
}
